<?php


namespace App\Shuixing\Controllers;


use App\Models\Posts\WxPost;
use App\Models\Posts\WxUserCircle;
use App\Models\WxCircle;
use App\Models\WxPlate;
use App\Models\WxSlideshow;
use App\Models\WxUser;

class CircleController
{
    public function index($id = -1)
    {

        View()->composer('shuixing/home', function ($view) {
            $view->with(['menu' => 2]);
        });

        //轮播广告
        $sdata = WxSlideshow::where('slideshow_state', 1)
            ->orderBy('sort', 'asc')
            ->get();

        //板块列表
        $plate = WxPlate::where('plate_state', 1)
            ->orderBy('sort', 'asc')
            ->get();

        //获取圈子列表
        if ($id == -1) {
            $data = WxCircle::where('circle_state', 1)
                ->where('is_hot', 1)
                ->orderBy('sort', 'asc')
                ->limit(8)->get();
            $data = self::circleParame($data);
        } else if ($id == 0) {
            $data = WxCircle::where('circle_state', 1)
                ->orderBy('id', 'desc')
                ->limit(10)->get();
            $data = self::circleParame($data);
        } else {
            $data = WxCircle::where('circle_state', 1)
                ->where('plate_id', $id)
                ->orderBy('sort', 'asc')
                ->get();
            $data = self::circleParame($data);
        }

        return view('shuixing/circle', ['data' => $data, 'plate' => $plate, 'sdata' => $sdata, 'type' => $id]);
    }

    public static function circleParame($data)
    {
        $data->map(function ($v) {
            $v->user_count = WxUserCircle::where('circle_id', $v->id)->count();
            $v->posts_count = WxPost::where('circle_id', $v->id)->count();
        });

        return $data;
    }
}
